package com.linx.lounge.service.impl;

import com.linx.common.core.utils.MapstructUtils;
import com.linx.common.core.utils.StringUtils;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.linx.lounge.domain.bo.LoungeFacilityBo;
import com.linx.lounge.domain.vo.LoungeFacilityVo;
import com.linx.lounge.domain.LoungeFacility;
import com.linx.lounge.mapper.LoungeFacilityMapper;
import com.linx.lounge.service.ILoungeFacilityService;

import java.util.List;
import java.util.Collection;

/**
 * 休息室-设施Service业务层处理
 *
 * @author linx
 * @date 2024-10-22
 */
@RequiredArgsConstructor
@Service
public class LoungeFacilityServiceImpl implements ILoungeFacilityService {

    private final LoungeFacilityMapper baseMapper;

    /**
     * 查询休息室-设施
     *
     * @param loungeFacilityId 主键
     * @return 休息室-设施
     */
    @Override
    public LoungeFacilityVo queryById(Long loungeFacilityId){
        return baseMapper.selectVoById(loungeFacilityId);
    }

    /**
     * 分页查询休息室-设施列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 休息室-设施分页列表
     */
    @Override
    public TableDataInfo<LoungeFacilityVo> queryPageList(LoungeFacilityBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LoungeFacility> lqw = buildQueryWrapper(bo);
        Page<LoungeFacilityVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的休息室-设施列表
     *
     * @param bo 查询条件
     * @return 休息室-设施列表
     */
    @Override
    public List<LoungeFacilityVo> queryList(LoungeFacilityBo bo) {
        LambdaQueryWrapper<LoungeFacility> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LoungeFacility> buildQueryWrapper(LoungeFacilityBo bo) {
        LambdaQueryWrapper<LoungeFacility> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFacilityName()), LoungeFacility::getFacilityName, bo.getFacilityName());
        lqw.eq(StringUtils.isNotBlank(bo.getFacilityCode()), LoungeFacility::getFacilityCode, bo.getFacilityCode());
        lqw.eq(bo.getStatus() != null, LoungeFacility::getStatus, bo.getStatus());
        lqw.eq(bo.getMeetingRoomId() != null, LoungeFacility::getMeetingRoomId, bo.getMeetingRoomId());
        lqw.like(StringUtils.isNotBlank(bo.getBrandName()), LoungeFacility::getBrandName, bo.getBrandName());
        lqw.eq(StringUtils.isNotBlank(bo.getSpec()), LoungeFacility::getSpec, bo.getSpec());
        lqw.eq(StringUtils.isNotBlank(bo.getResponsiblePerson()), LoungeFacility::getResponsiblePerson, bo.getResponsiblePerson());
        lqw.eq(StringUtils.isNotBlank(bo.getResponsiblePersonTelephone()), LoungeFacility::getResponsiblePersonTelephone, bo.getResponsiblePersonTelephone());
        return lqw;
    }

    /**
     * 新增休息室-设施
     *
     * @param bo 休息室-设施
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(LoungeFacilityBo bo) {
        LoungeFacility add = MapstructUtils.convert(bo, LoungeFacility.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLoungeFacilityId(add.getLoungeFacilityId());
        }
        return flag;
    }

    /**
     * 修改休息室-设施
     *
     * @param bo 休息室-设施
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(LoungeFacilityBo bo) {
        LoungeFacility update = MapstructUtils.convert(bo, LoungeFacility.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LoungeFacility entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除休息室-设施信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
